package com.fingard.dsp.bank.directbank.aibank01;

import com.alibaba.fastjson.JSON;
import com.fingard.dsp.bank.directbank.aibank01.request.SendRequest;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.kayakwise.sgp.object.request.cnaps.SeLiqu0001Req;
import com.kayakwise.sgp.object.request.cnaps.SeLiqu0002Req;
import com.kayakwise.sgp.object.response.cnaps.SeLiqu0001Resp;
import com.kayakwise.sgp.object.response.cnaps.SeLiqu0002Resp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AIBANK01Balance extends AIBANK01Base implements IBalance {

    ApplicationContext ac = new ClassPathXmlApplicationContext("classpath*:dsp-config/spring-config.xml");
    SendRequest sendRequest = (SendRequest) ac.getBean("SendRequest");
    private final String S00 = "S00";
    private final String I05 = "I05";
    private final String I06 = "I06";
    private final String I07 = "I07";

    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        while (p_req7506.readRecord()) {
            if (p_req7506.readedDetail.higherLevelAct == null || p_req7506.readedDetail.higherLevelAct.length() == 0) {
                try {
                    RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();

                    SeLiqu0001Req seLiqu0001Req = new SeLiqu0001Req();
                    seLiqu0001Req.setReq_type("01");
                    seLiqu0001Req.setQrytp("QT00");
                    WriteBankLogLn("1请求的对象参数" + JSON.toJSONString(seLiqu0001Req));
                    SeLiqu0001Resp seLiqu0001Resp = sendRequest.sendToBank(seLiqu0001Req);
                    //     WriteBankLogLn("请求的对象参数" + JSON.toJSONString(seLiqu0001Req));

                    WriteBankLogLn("1返回的对象结果seLiqu0001Resp：" + JSON.toJSONString(seLiqu0001Resp));
                    if (S00.equals(seLiqu0001Resp.getStep_status()) || I05.equals(seLiqu0001Resp.getStep_status()) ) {
                        //等待初始化数据
                        Thread.sleep(Long.valueOf(getBankConfig().getValueAsString("sleeptime")));
                        SeLiqu0002Req seLiqu0002Req = new SeLiqu0002Req();
                        seLiqu0002Req.setOri_busi_serno(seLiqu0001Req.getHead().getTransId());
                        seLiqu0002Req.setOri_sys_serno(seLiqu0001Req.getHead().getSubTransSeq());
                        seLiqu0002Req.setOri_req_date(seLiqu0001Req.getHead().getSysDate());
                        seLiqu0002Req.setReq_type("01");
                        WriteBankLogLn("2请求的对象参数" + JSON.toJSONString(seLiqu0002Req));

                        SeLiqu0002Resp seLiqu0002Resp = sendRequest.sendToBank(seLiqu0002Req);
                        //          WriteBankLogLn("请求的对象参数" + JSON.toJSONString(seLiqu0002Req));

                        WriteBankLogLn("2返回的对象结果seLiqu0002Resp：" + JSON.toJSONString(seLiqu0002Resp));
                        if (I05.equals(seLiqu0002Resp.getStep_status())) {
                            tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.currBal = String.valueOf(seLiqu0002Resp.getBal_amount());
                            tmpRetDetail.availBal = String.valueOf(seLiqu0002Resp.getBal_amount().subtract(seLiqu0002Resp.getCdtld()));
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo("结果查询状态：" + seLiqu0002Resp.getStep_status());
                        }
                        boolean isSuccess = false;
                        //构建请求model
                        SeLiqu0001Req request0001 = new SeLiqu0001Req();
                        //01 - 清算账户信息查询申请  02 - 净借记限额查询申请  03 - 净借记限额圈存资金调增/调减申请
                        request0001.setReq_type("02");
                        //   request0001.setQrytp("QT00");
                        //         WriteBankLogLn("开始请求银行接口，参数值" + JSON.toJSONString(request0001));
                        SeLiqu0001Resp resp0001 = sendRequest.sendToBank(request0001);
                        WriteBankLogLn("3开始请求银行接口，参数值" + JSON.toJSONString(request0001));
                        if(resp0001!=null) {
                            String msgInfo = resp0001.getStep_status();
                            System.out.println("银行返回结果状态码：" + resp0001.getStep_status());
                            if(msgInfo!=null && msgInfo.equals("S00")) {
                                isSuccess = true;
                                WriteBankLogLn("清算类交易申请成功，开始处理清算类交易申请结果查询。。。");
                            }
                        }
                        //清算类交易申请结果查询
                        if(isSuccess) {
                            SeLiqu0002Req request002 = new SeLiqu0002Req();
                            //原全局流水号
                            request002.setOri_busi_serno(request0001.getHead().getTransId());
                            //原请求日期
                            request002.setOri_req_date(request0001.getHead().getSysDate());
                            //原子交易序号
                            request002.setOri_sys_serno(request0001.getHead().getSubTransSeq());
                            //交易类型
                            request002.setReq_type("02");
                            //延迟等待
                            Thread.sleep(Long.valueOf(getBankConfig().getValueAsString("sleeptime")));
                            WriteBankLogLn("4请求的对象参数" + JSON.toJSONString(request002));

                            SeLiqu0002Resp resp0002 = sendRequest.sendToBank(request002);

                            WriteBankLogLn("5返回的对象结果resp0002：" + JSON.toJSONString(resp0002));
                            if(resp0002!=null) {
                                String tmpRetCode = resp0002.getStep_status();
                                String tmpRetInfo = resp0002.getRjctinf();
                                if(tmpRetCode.equals("I05")){
                                    tmpRetDetail.frozenBal = String.valueOf(resp0002.getNetbtlmt().subtract(resp0002.getAvlblnetdbt()));
                                }
                                else{
                                    retAts.writeStart();
                                    retAts.writeRespCode("-1");
                                    retAts.writeRespInfo(tmpRetInfo);
                                }
                            }
                            else{
                                retAts.writeStart();
                                retAts.writeRespCode("-1");
                                retAts.writeRespInfo("交易失败");
                            }
                        }else{
                            retAts.writeStart();
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo("交易失败");
                        }
                        retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);

                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo("申请状态：" + seLiqu0001Resp.getStep_status());
                    }
                } catch (Exception e) {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(e.getMessage());
                    e.printStackTrace();
                    WriteBankLogLn("交易出现异常:" + e);
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo("影子户不可查询余额!");
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
